package com.lay.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lay.service.*;
import com.lay.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Controller
@RequestMapping("/fuwu")
public class FuwuGuanliController {
    @Autowired
    private DeptService deptService;
    @Autowired
    private CarInsurService carInsurService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private CarsService carsService;
    @Autowired
    private InsurContractService insurContractService;
    @Autowired
    private ClaimantService claimantService;
    /**
     * @title
     * @description 跳转到支点查询页面
     * @author admin
     * @param
     * @updateTime 2022/6/14 10:17
     * @return

     */
    @RequestMapping("/tomap")
    public ModelAndView tomap(){
        ModelAndView mv =new ModelAndView();
        List<Dept> list = deptService.list(null);
        mv.addObject("list",list);
        mv.setViewName("map");
        return mv;
    }
    /**
     * @title
     * @description 跳转到计算器页面
     * @author admin
     * @param
     * @updateTime 2022/6/14 10:18
     * @return

     */
    @RequestMapping("/cheXian")
    public ModelAndView cheXian(){
        return new ModelAndView("cheXianJs");
    }

    @RequestMapping("/touser-msg")
    public ModelAndView userMsg(HttpSession session){
        ModelAndView mv = new ModelAndView();
        //用户信息
        UserNumber userNumber = (UserNumber) session.getAttribute("users");
        Users users = usersService.getById(userNumber.getuId());
        mv.addObject("user",users);

        //车辆信息
        QueryWrapper qw = new QueryWrapper();
        qw.eq("u_id",userNumber.getuId());
        List<Cars> list = carsService.list(qw);
        mv.addObject("list",list);
        //订单信息
        List<InsurContract> list1 = insurContractService.queryByUid(userNumber.getuId());
        mv.addObject("list1",list1);

        mv.setViewName("user-msg");
        return mv;
    }
    /**
     * @title
     * @description 计算车险
     * @author admin
     * @param
     * @updateTime 2022/6/14 10:18
     * @return

     */
    @RequestMapping("/baojia")
    public ModelAndView chexianjs(String jiage, int fangan){
        ModelAndView mv = new ModelAndView() ;
        BigDecimal jiage1 = new BigDecimal(jiage);
        if (fangan == 0){
            fangan =1;
        }
        List<Integer> list = new ArrayList<>();
        //按照基础方案报价
        if (fangan == 1){
            list.clear();
            list.add(1);
            list.add(5);
            list.add(8);
            List<CarInsur> carInsurs = (List<CarInsur>) carInsurService.listByIds(list);
            BigDecimal bg = new BigDecimal(0);
            for (CarInsur c:carInsurs){
                if (!c.getCiName().equals("机动车交通事故责任强制保险")){
                    BigDecimal b1= new BigDecimal(c.getInsRale()).multiply(jiage1).add(c.getCiMoney());
                    bg = bg.add(b1);
                }

            }
            mv.addObject("list",carInsurs);
            mv.addObject("shangye",bg);
        }else if(fangan == 2){
            list.clear();
            list.add(1);
            list.add(5);
            list.add(8);
            list.add(9);
            List<CarInsur> carInsurs = (List<CarInsur>) carInsurService.listByIds(list);
            BigDecimal bg = new BigDecimal(0);
            for (CarInsur c:carInsurs){
                if (!c.getCiName().equals("机动车交通事故责任强制保险")){
                    BigDecimal b1= new BigDecimal(c.getInsRale()).multiply(jiage1).add(c.getCiMoney());
                    bg= bg.add(b1);
                }
        }
            mv.addObject("list",carInsurs);
            mv.addObject("shangye",bg);
    }else {
            list.clear();
            list.add(1);
            list.add(5);
            list.add(8);
            list.add(9);
            list.add(21);
            List<CarInsur> carInsurs = (List<CarInsur>) carInsurService.listByIds(list);
            BigDecimal bg = new BigDecimal(0);
            for (CarInsur c:carInsurs){
                if (!c.getCiName().equals("机动车交通事故责任强制保险")){
                    BigDecimal b1= new BigDecimal(c.getInsRale()).multiply(jiage1).add(c.getCiMoney());
                    bg=  bg.add(b1);
                }

            }
            mv.addObject("list",carInsurs);
            mv.addObject("shangye",bg);
        }
        mv.addObject("jiage",jiage);
        mv.setViewName("chexianjisuan");
        return mv;
    }
    @RequestMapping("/xuBao")
    public ModelAndView xuBao(int id , HttpSession session){
        ModelAndView mv = new ModelAndView();
        InsurContract insurContract = insurContractService.queryByIcId(id);
        mv.addObject("insurContract",insurContract);

        BigDecimal icMoney = insurContract.getIcMoney();

        //用户信息
        UserNumber userNumber = (UserNumber) session.getAttribute("users");
        Users users = usersService.getById(userNumber.getuId());
        mv.addObject("user",users);

        //用户积分
        int jifen = users.getuJifen();

        if (jifen < 200){
            mv.addObject("money",icMoney);
        }else if (jifen < 400){
            icMoney= icMoney.multiply(new BigDecimal(0.9));
            mv.addObject("money",icMoney);
        }else if (jifen < 600){
            icMoney= icMoney.multiply(new BigDecimal(0.8));
            mv.addObject("money",icMoney);
        }else if (jifen < 800){
            icMoney= icMoney.multiply(new BigDecimal(0.7));
            mv.addObject("money",icMoney);
        }else if (jifen < 1000){
            icMoney= icMoney.multiply(new BigDecimal(0.6));
            mv.addObject("money",icMoney);
        }
        mv.setViewName("xubao");
        return mv;
    }
    //报案信息
    @RequestMapping("/tobaoanxinxi-list")
    public ModelAndView baoanList(HttpSession session){
        ModelAndView mv = new ModelAndView();
        UserNumber userNumber = (UserNumber) session.getAttribute("users");
        QueryWrapper qw = new QueryWrapper();
        qw.eq("u_id",userNumber.getuId());
        qw.orderByDesc("cl_addtime");
        List<Claimant> list = claimantService.list(qw);
        mv.addObject("list",list);
        mv.setViewName("baoanxinxi-list");
        return mv;
    }
}
